//
// Created by Zhang on 2022/3/24.
//
#include <iostream>
#include <string>

// book chapter 1 q6
class Crectangle {
private:
    double x = 0;
    double y = 0;
    double length;
    double height;
public:
    void Move(double, double);

    void Size(double l, double h);

    void Where() const;

    double Area() const;
};

void Crectangle::Move(double x1, double y1) {
    x += x1;
    y += y1;
}

void Crectangle::Size(double l, double h) {
    length = l;
    height = h;
}

void Crectangle::Where() const {
    std::cout << "x:" << x - length / 2 << "; y:" << y + height / 2 << std::endl;
}

double Crectangle::Area() const {
    return height * length;
}

//chapter 2 q1
class Student_Q1 {
private:
    long id;
    float score = 100;
public:
    Student_Q1();

    void out();
};

void Student_Q1::out() {
    std::cout << "id:" << id << " score:" << score << std::endl;
}

Student_Q1::Student_Q1() {
    static long i = 1;
    id = i++;
}

class Student {
    std::string name;
    int score;
public:
    Student(std::string name_init, int score_init);

    friend void compare_student(Student a, Student b);
};

Student::Student(std::string name_init, int score_init) : name(name_init), score(score_init) {
}

void compare_student(Student a, Student b) {
    if (a.score > b.score) {
        std::cout << "high:" << a.name << "\nlow:" << b.name << std::endl;

    } else {
        std::cout << "high:" << b.name << "\nlow:" << a.name << std::endl;
    }
}

class Complex {
private:
    double real;
    double imaginary;

public:
    Complex(double r, double i) : real(r), imaginary(i) {}

    friend Complex add(Complex a, Complex b);

    void show();
};

Complex add(Complex a, Complex b) {
    return Complex(a.real + b.real, a.imaginary + b.imaginary);
}

void Complex::show() {
    std::cout << real << "+i*" << imaginary << std::endl;
}

int main() {
    //book chapter 1 q6
    Crectangle a6;
    a6.Move(2.3, 7.9);
    a6.Size(9.3, 6.0);
    a6.Where();
    std::cout << a6.Area();

//book chapter 2 q1
    auto p = new Student_Q1[5];
    for (int j = 0; j < 4; ++j) {
        (2 * j - 2)[p].out();
    }
//book chapter 2 q2
    Student a("Misaka", 100), b("Kamijou", 99);
    compare_student(a, b);

    //book chapter 2 q3
    Complex a3(1, 2), b3(3, 4);
    add(a3, b3).show();

    return 0;
}

